package com.windupurnomo;

import java.util.List;
import java.util.Random;

import com.windupurnomo.data.ActivityManagerData;
import com.windupurnomo.data.DataModel;
import com.windupurnomo.entity.Player;
import com.windupurnomo.entity.ShareInfo;
import com.windupurnomo.util.Function;
import com.windupurnomo.util.MyQuery;
import com.windupurnomo.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;



public class PlayerProfile extends Activity implements OnClickListener{
	private ShareInfo shareInfo = new ShareInfo();
	RadioButton radioMale, radioFemale;
	ImageView image = null;
	EditText name = null;
	Button btnNext = null;
	String sex = null;
	DataModel dataModel = null;
	SQLiteDatabase db = null;
	ActivityManagerData acm = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_profile);
        acm = new ActivityManagerData(this);
        db = acm.getWritableDatabase();
		acm.createTable(db);
		acm.genDataResource(db);
        
        name = (EditText) findViewById(R.id.txtName);
        radioMale = (RadioButton) findViewById(R.id.radio_male);
        radioFemale = (RadioButton) findViewById(R.id.radio_female);
        image = (ImageView) findViewById(R.id.imageAvatar);
        btnNext = (Button) findViewById(R.id.btn_Next);
        
        radioMale.setOnClickListener(this);
        radioFemale.setOnClickListener(this);
        btnNext.setOnClickListener(this);
    }

	
	public void onClick(View v) {
        switch (v.getId()) {
			case R.id.radio_male: image.setImageResource(R.drawable.male); sex = "Male"; break;
			case R.id.radio_female: image.setImageResource(R.drawable.female); sex = "Female"; break;
			case R.id.btn_Next: {
				getGameDays();
				shareInfo.setPlayerStatus(Util.Player.NEW_PLAYER);
				savePlayer();
				break;
			}
		}
	}
	
	public void savePlayer(){
		Player p = new Player();
		p = Function.defaultResConPlayer(p);//set default resource and condition
		String name = this.name.getText().toString();
		p.setName(name);
		p.setSex(sex);
		p.setScore(0);
		long success = acm.generateData(db, p);
		p.setId((int) success);
		shareInfo.setPlayer(p);
		if(success!=-1){
			Intent i = new Intent(this, ViewDay.class);
			i.putExtra("shareInfo", shareInfo);
			startActivity(i);
		}
	}
	
	private void getGameDays(){
		Random r = new Random();
		int idGame = r.nextInt(5);
		idGame = 1;//karena sekarang baru ada satu game
		ActivityManagerData acm = new ActivityManagerData(this);
        SQLiteDatabase db = acm.getWritableDatabase();
		List<Integer> listDay = MyQuery.GameDAO.getGameDays(idGame, db);
		shareInfo.setListDay(listDay);
		shareInfo.setIndexListDay(0);
	}
}